/*
 * Created to handle all the profile page's javascript need
 * Author: Yong Qui Zheng
 * Initial Verison: 12/12/2014
 * Change:
 * Added the editable initialization - 12/12/2014 
 */
$(document).ready(function() {
    /*initialize the editable*/
    $('td.editable_field a.editable_link').editable({
        type:'text',
        send:'always',
        validate:function(value){
           if($.trim(value) == '') {
                return 'This field is required';
            }
        },
        placement:"right",
        url:function(params){
            var d = new $.Deferred;
            var table = $(this).attr("data-table");
            var data = {val:params.value,name:params.name,table:table};
            $.ajax({
                url:"profile/editdata",
                data:data,
                dataType:"json",
                success:function(result,success){
                    if(success==="success" && result.success===true){
                        d.resolve();
                    }else{
                        return d.reject("Edit field failed");
                    }
                    return d.promise();
                },
                type:'POST'
            });



        }
    });
    // selectable
    $("td.editable_field a.selectable_link").editable({
        type:'select',
        send:'always',
        placement:'top',
        url:function(params){
            var d = new $.Deferred; 
            var table = $(this).attr("data-table");
            var data = {val:params.value,name:params.name,table:table,pk:params.pk};
            $.ajax({
                url:"profile/editselectdata",
                data:data,
                dataType:"json",
                success:function(result,success){
                     if(success==="success" && result.success===true){
                        d.resolve();
                    }else{
                        return d.reject("Edit field failed");
                    }
                    return d.promise();
                },
                type:'POST'
            });
        }
    });

    //date
    $("td.editable_field a.date_link").editable({
        type:'date',
        send:'never',
        placement:'top',
        format:'YYYY-MM-DD',    
         viewformat: 'MM/DD/YYYY',
         template: 'MMM / D / YYYY',
        url:function(params){
            var d = new $.Deferred;
            var table = $(this).attr("data-table");
            var data = {val:params.value,name:params.name,table:table,pk:params.pk};

            $.ajax({
                url:"profile/editselectdata",
                data:data,
                dataType:"json",
                success:function(result,success){
                    if(success==="success" && result.success===true){
                        d.resolve();
                    }else{
                        return d.reject("Edit field failed");
                    }
                    return d.promise();
                },
                type:'POST'
            });
        }
    });

    $(".profile_add_form .dynamic_selector").each(function(){
       var dataSourceUrl = $(this).attr("data-source"); 
       var $curSelect = $(this);
       if(typeof dataSourceUrl !=='undefined' && dataSourceUrl!==""){
           $.get(dataSourceUrl,function(resp,status){
                 if(status==='success'){
                     var response=$.parseJSON(resp);
                     for(var i=0;i<response.length;i++){
                         var single=response[i];
                         var html = "<option value=\""+single.value+"\">"+single.text+"</option>";
                         $curSelect.append(html);
                     }
                 }
           });
       }
    });
    $('.datepicker').datepicker();

    $(".profile_add_form").submit(function(e){
       e.preventDefault();
       var url=$(this).attr("action");
       var data=$(this).serialize();
       $.ajax({
           url:url,
           data:data,
           type:'POST',
           dataType:'json',
           success:function(resp,success){
               if(success==='success'){
                   if(resp.success){
                       window.location.reload();                    
                   }
               }
           }
       });
    });
    
    //bind the clear button
    $(".clear_icon").click(function(){
        var resp = confirm("Are you sure you want to delete?");
        if(resp===true){
            var table = $(this).attr("data-table");
            var pk = $(this).attr("data-pk");
            var data={'table':table,'pk':pk};
            $.ajax({
                url:'profile/removeData',
                data:data,
                type:'POST',
                dataType:'json',
                success:function(resp,success){
                    if(success==='success'){
                        if(resp.success){
                            window.location.reload();
                        }
                    }
                }
            });
        }
        
    });
     
});


